package flaud

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/flaud"
	flaudReq "github.com/flipped-aurora/gin-vue-admin/server/model/flaud/request"
)

type JudgmentReasonService struct{}

// CreateJudgmentReason 创建裁判理由管理记录
// Author [yourname](https://github.com/yourname)
func (jrgService *JudgmentReasonService) CreateJudgmentReason(ctx context.Context, jrg *flaud.JudgmentReason) (err error) {
	err = global.GVA_DB.Create(jrg).Error
	return err
}

// DeleteJudgmentReason 删除裁判理由管理记录
// Author [yourname](https://github.com/yourname)
func (jrgService *JudgmentReasonService) DeleteJudgmentReason(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&flaud.JudgmentReason{}, "id = ?", ID).Error
	return err
}

// DeleteJudgmentReasonByIds 批量删除裁判理由管理记录
// Author [yourname](https://github.com/yourname)
func (jrgService *JudgmentReasonService) DeleteJudgmentReasonByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]flaud.JudgmentReason{}, "id in ?", IDs).Error
	return err
}

// UpdateJudgmentReason 更新裁判理由管理记录
// Author [yourname](https://github.com/yourname)
func (jrgService *JudgmentReasonService) UpdateJudgmentReason(ctx context.Context, jrg flaud.JudgmentReason) (err error) {
	err = global.GVA_DB.Model(&flaud.JudgmentReason{}).Where("id = ?", jrg.ID).Updates(&jrg).Error
	return err
}

// GetJudgmentReason 根据ID获取裁判理由管理记录
// Author [yourname](https://github.com/yourname)
func (jrgService *JudgmentReasonService) GetJudgmentReason(ctx context.Context, ID string) (jrg flaud.JudgmentReason, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&jrg).Error
	return
}

// GetJudgmentReasonInfoList 分页获取裁判理由管理记录
// Author [yourname](https://github.com/yourname)
func (jrgService *JudgmentReasonService) GetJudgmentReasonInfoList(ctx context.Context, info flaudReq.JudgmentReasonSearch) (list []flaud.JudgmentReason, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&flaud.JudgmentReason{})
	var jrgs []flaud.JudgmentReason
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&jrgs).Error
	return jrgs, total, err
}
func (jrgService *JudgmentReasonService) GetJudgmentReasonPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
